<?php
namespace app\admin\model;
use think\Exception;
use think\Model;
use think\Db;
class Login extends Model{
    public function login(){
        $input = input();
        if(!$this->validate()->validateData($input)){
            throw new Exception($this->getError());
        }
        $dbLogin = $this->getByLogin($input['login']);
        if(empty($dbLogin)){
            throw new Exception("账户不存在");
        }
        if ($dbLogin['password']!=$input['password']){
            throw new Exception("密码错误");
        }
        return User1::get($dbLogin['id']);
    }
    public function  getMenuData($roleId){
        $dbDatas=Db::query("select id,title,path,pid from useable join access on useable.id = access.useable_id where visible='y' and role_id=$roleId");
        $tmpData = array(array());//给pid的一个父级 也就是0的id
        foreach ($dbDatas as &$row){
            $tmpData[$row['id']]=&$row;//将id转化为数组的索引
            unset($row['id']);
        }
        foreach ($dbDatas as &$row){
            $tmpData[$row['pid']]['children'][]=&$row;//通过pid找到他的父级
            unset($row['pid']);
        }
//        通过这样就可以拿到数组的父子级目录
        return $tmpData[0]['children'];
    }
    function getAccessUrls($roleId){//拿到可以访问的路径
        $tmps = $this->query("select path from useable join access on useable.id = access.useable_id where role_id=$roleId");
        $urls = array();
        foreach ($tmps as &$row){//避免占用内存空间循环存入数组里面
            $urls[$row['path']]=true;
        }
        return $urls;
    }
}
